import axios from 'axios'
import store from '@/store'
import { Toast } from 'vant'
import { getBaseURL } from './baseURL'
const baseURL = getBaseURL('videoBaseURL')
const request = axios.create({
  baseURL: baseURL + '/api/private/v1'
})

request.interceptors.request.use(
  function (config) {
    const user = store.state.user
    if (user) {
      config.headers.Authorization = user.token
    }
    // Do something before request is sent
    return config
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error)
  }
)

request.interceptors.response.use(
  response => {
    return response
  },
  error => {
    Toast('认证失效，请重新登陆')
    store.commit('removeUser')
    return Promise.reject(error)
  }
)

export default request
